package com.test.user.service.impl;

import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.user.domain.dto.UserRepDto;
import com.test.user.domain.po.User;
import com.test.user.mapper.UserMapper;
import com.test.user.service.IUserService;
import com.test.user.utils.JwtTool;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
@RequiredArgsConstructor
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    private final JwtTool jwtTool;
    @Override
    public String login(UserRepDto userRepDto) {
        User user = getOne(Wrappers.<User>lambdaQuery().eq(User::getMobile, userRepDto.getMobile())
                .eq(User::getPwd, userRepDto.getPwd()));
        if(user==null){
            throw new RuntimeException("用户名或密码错误");
        }
        String token = jwtTool.createToken(user.getId(), Duration.ofHours(2));
        return token;
    }
}
